package com.vipheyue.bigbusiness.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lidroid.xutils.util.LogUtils;
import com.vipheyue.bigbusiness.R;
import com.vipheyue.bigbusiness.bean.FavoritesTable;
import com.vipheyue.bigbusiness.bean.Job;
import com.vipheyue.bigbusiness.utils.GsonUtils;
import com.vipheyue.bigbusiness.utils.MyDb;
import com.vipheyue.bigbusiness.view.CommonAdapter;
import com.vipheyue.bigbusiness.view.ViewHolder;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.bmob.v3.BmobQuery;
import cn.bmob.v3.listener.FindListener;

public class FavoritesActivity extends AppCompatActivity {
    @Bind(R.id.rl_menu_left)
    RelativeLayout rl_menu_left;
    @Bind(R.id.tv_Menu_Title)
    TextView tv_Menu_Title;
    @Bind(R.id.iv_menu_right)
    ImageView iv_menu_right;
    @Bind(R.id.rl_menu_right)
    RelativeLayout rl_menu_right;
    @Bind(R.id.listView)
    ListView listView;
    /*  * 数据中心
           */
    private List<Job> mDatas = new ArrayList<Job>();
    private CommonAdapter<Job> mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_favorites);
        ButterKnife.bind(this);
//        StatusBarCompat.compat(this, 0xFF00ABEC);

//        setContentView(R.layout.test);
        setAdapter();
        initData();
    }

    private void setAdapter() {

        //mdatas 记得加数据进去
        mAdapter = new CommonAdapter<Job>(this, mDatas, R.layout.job_item) {
            @Override
            public void convert(ViewHolder helper, int position, Job item) {
                helper.setText(R.id.tv_jobTitile, item.getJobTitle());
                helper.setRoundText(R.id.rlv_jobType, item.getJobType());
                helper.setText(R.id.tv_workAreaType, item.getWorkAreaType());
                helper.setText(R.id.tv_createTime, item.getCreatedAt());
                helper.setText(R.id.tv_maxSalaryAndType, item.getMaxSalary() + "/" + item.getSalaryType());

            }

        };
        listView.setOnItemClickListener(new ItemClickListener());
        listView.setAdapter(mAdapter);
    }

    private void initData() {
        tv_Menu_Title.setText("我的收藏");
        BmobQuery<Job> query = new BmobQuery<Job>();
//        query.setLimit(10); // 限制最多10条数据结果作为一页
//        query.setSkip(10 * getNumPage[0]); // 忽略前10条数据（即第一页数据结果）
        List<FavoritesTable> FavoritesTables = MyDb.getObjectTable(this);
        if (FavoritesTables == null || FavoritesTables.size() == 0) {
            Toast.makeText(FavoritesActivity.this, "没有数据", Toast.LENGTH_SHORT).show();
            return;
        }
        ArrayList<String> stringArrayList = new ArrayList<>();
        for (FavoritesTable favoritesTable1 : FavoritesTables) {
            stringArrayList.add(favoritesTable1.getJobId());
            LogUtils.i("------>  " + favoritesTable1.getJobId());
        }
        String[] stringArray = new String[stringArrayList.size()];
        String[] ids = stringArrayList.toArray(stringArray);//TODO 把list生成的Array放到[]里面

        query.addWhereContainedIn("objectId", Arrays.asList(ids));
        query.include("publisherCompany");// 希望在查询帖子信息的同时也把发布人的信息查询出来
        query.findObjects(this, new FindListener<Job>() {
            @Override
            public void onSuccess(List<Job> object) {
                mDatas.addAll(object);
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(int code, String msg) {
            }
        });


    }
    private final class ItemClickListener implements AdapterView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            ListView listView = (ListView) parent;
//            ProductListBean.DataEntity.DataEntity2.ListEntity bean = (ProductListBean.DataEntity.DataEntity2.ListEntity) listView.getItemAtPosition(position);
            Job job= mDatas.get(position);
            String jobStr=  GsonUtils.toJson(job);
            Intent intent = new Intent(FavoritesActivity.this, JobDetailActivity.class);
            intent.putExtra("jobStr", jobStr);
            startActivity(intent);
        }
    }
    @OnClick(R.id.rl_menu_left)
    public void onClick() {
        finish();
    }
}
